package com.sbs.gaoxinqutoilet.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;

import com.google.android.material.tabs.TabLayout;
import com.sbs.gaoxinqutoilet.R;
import com.sbs.gaoxinqutoilet.adapter.TongjiPagerAdapter;
import com.sbs.gaoxinqutoilet.base.BaseBackFragment;
import com.sbs.gaoxinqutoilet.entity.HomeMenuEntity;
import com.sbs.gaoxinqutoilet.eventbus.EventCenter;
import com.sbs.gaoxinqutoilet.interfaces.EventConstant;
import com.sbs.gaoxinqutoilet.utils.LogUtils;
import com.sbs.gaoxinqutoilet.view.MyViewPager;
import com.zhy.autolayout.AutoRelativeLayout;
import com.zhy.http.okhttp.OkHttpUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by 聪明一只哈 on 2018/6/25.
 * Time: 2018/6/25  16:56
 */

public class ToiletStatisTotalFragment extends BaseBackFragment {

    //传过来的改造档案数据  存储在Bundle中
    private static final String STATISTICAL_FILE = "statistical_file";
    @BindView(R.id.toolbar_back)
    ImageView toolbarBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.toolbar_iv_menu)
    ImageView toolbarIvMenu;
    @BindView(R.id.toolbar_tv_menu)
    TextView toolbarTvMenu;
    @BindView(R.id.toolbar_rl_menu)
    AutoRelativeLayout toolbarRlMenu;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.myviewpager)
    MyViewPager myViewpager;
    Unbinder unbinder;
    private HomeMenuEntity homemenu_buildfile;

    private List<Fragment> mFragments = new ArrayList<Fragment>();
    private List<String> mtitles = new ArrayList<String>();
    private ToiletStatisTicalFragmentD t1,t2;
    private TongjiPagerAdapter pagerAdapter;

    public static ToiletStatisTotalFragment newInstance(HomeMenuEntity homeMenuEntity) {
        Bundle args = new Bundle();
        args.putSerializable(STATISTICAL_FILE, homeMenuEntity);
        ToiletStatisTotalFragment fragment = new ToiletStatisTotalFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_totalstatistical, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView(inflater);
        initListener();

        return attachToSwipeBack(view);
    }


    private void initView(LayoutInflater inflater) {
        EventBus.getDefault().register(this);
        toolbarTitle.setText("改厕进度统计");
        //返回按钮
        toolbarBack.setVisibility(View.VISIBLE);
        //设置回键
        initToolbarBack(toolbar);



        //获取传递过来的值
        Bundle arguments = getArguments();
        if (arguments != null) {
            homemenu_buildfile = (HomeMenuEntity) arguments.getSerializable(STATISTICAL_FILE);
            if (homemenu_buildfile != null) {
                Log.i("统计页面", "initView: " + homemenu_buildfile.getFunc_name() + "~~~" + homemenu_buildfile.getFunc_id());
                //showShortToast(homemenu_buildfile.getFunc_name()+"~~~"+homemenu_buildfile.getFunc_id());
            }

        }

//        t1 = new ToiletStatisTicalFragmentD();
//        t1.setIud(homemenu_buildfile.getIud());
//        t1.setNianfen("2019");
//        t2 = new ToiletStatisTicalFragmentD();
//        t2.setIud(homemenu_buildfile.getIud());
//        t2.setNianfen("2018");

        mFragments.add(t1);
        mFragments.add(t2);

        mtitles.add("2019");
        mtitles.add("2018");






    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        pagerAdapter = new TongjiPagerAdapter(getChildFragmentManager(), mtitles, mFragments);

        myViewpager.setAdapter(pagerAdapter);

       // myViewpager.setOffscreenPageLimit(mFragments.size());
        //为TabLayout设置ViewPager
        tabLayout.setupWithViewPager(myViewpager);



    }

    private void initListener() {




    }


    /**
     * eventbus 页面跳转,事件接受
     *
     * @param center
     */
    @Subscribe
    public void onEventReceive(EventCenter center) {

        if (center.getEventCode() == EventConstant.UPLOAD_BUY_MODIFY) {
            //提交成功
            LogUtils.e("显示", "刷新");
            new Handler() {
            }.postDelayed(new Runnable() {
                @Override
                public void run() {
                    // requestRefresh();
                }
            }, 500);
        }
    }

    /**
     * 页面加载动画结束就调用  loadMoreData(index, pageSize);
     * 所以  将两个方法紧挨着
     *
     * @param savedInstanceState
     */
    @Override
    public void onEnterAnimationEnd(Bundle savedInstanceState) {
        super.onEnterAnimationEnd(savedInstanceState);
        //添加家在逻辑
       // requestRefresh();


    }



    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        EventBus.getDefault().unregister(this);
        OkHttpUtils.getInstance().cancelTag(this);
        cancelLoadingDialog();
    }
}
